<%--
  Created by IntelliJ IDEA.
  User: ZhuChunXiao
  Date: 2017/6/1
  Time: 14:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">
  <div class="row">

    <div class="col-lg-12">
      <div class="panel">
        <div class="panel-heading">
          <h3 class="panel-title">订单管理</h3>
        </div>
        <div class="panel-body">
          <input type="hidden" id="chooseId" />
          <div class="form-inline">
            <div class="row">
              <div class="col-sm-12 table-toolbar-left">
                <c:if test="${viewtype==1}">
                  <btn id="btn_add_order" class="btn btn-primary btn-sm" ${isCreate}>
                    <i class="ti-plus"> </i> 新建订单申请
                  </btn>

                  <btn id="btn_cancel_order" class="btn btn-warning btn-sm pull-right">
                    <i class="ti-close"> </i> 订单取消
                  </btn>
                </c:if>
                <c:if test="${viewtype==2}">
                  <btn id="btn_track_order" class="btn btn-primary btn-sm">
                    <i class="ti-plus"> </i> 新建订单跟踪
                  </btn>
                  <btn id="btn_finish_order" class="btn btn-primary btn-sm">
                    <i class="ti-check"> </i> 订单完成
                  </btn>

                  <btn id="btn_suspend_order" class="btn btn-warning btn-sm pull-right" style="margin-left: 5px">
                    <i class="ti-control-pause"> </i> 订单暂停
                  </btn>
                  <btn id="btn_cancel_order" class="btn btn-warning btn-sm pull-right">
                    <i class="ti-close"> </i> 订单取消
                  </btn>
                </c:if>
                <c:if test="${viewtype==4}">
                  <btn id="btn_recovery_order" class="btn btn-warning btn-sm pull-right" style="margin-left: 5px">
                    <i class="ti-back-right"> </i> 订单恢复
                  </btn>

                  <btn id="btn_cancel_order" class="btn btn-warning btn-sm pull-right">
                    <i class="ti-close"> </i> 订单取消
                  </btn>
                </c:if>

              </div>
            </div>
          </div>

          <div class="table-responsive">
            <table id="order_implementation_table"></table>
            <div id="order_implementation_page"></div>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

<script>

  $(function(){

    init_grid_order();

  });

  function init_grid_order() {

    __init_jqgrid('order_implementation_table', 'order_implementation_page',
            '<%=path%>/com/orderEntity/list?viewtype=${viewtype}',
            [<c:if test="${viewtype==2}">'',</c:if>'订单编号', '订单客户', '订单合同', '订单下达日期', '期望交付日期'<c:if test="${viewtype==1}">, '当前操作人', '当前状态'</c:if><c:if test="${viewtype==2}">, '完成率', '最新跟踪时间'</c:if><c:if test="${viewtype==3}">, '订单交付日期'</c:if><c:if test="${viewtype==4}">, '暂停日期'</c:if><c:if test="${viewtype==5}">, '取消日期'</c:if>, '操作', 'id'],
            [
              <c:if test="${viewtype==2}">
              {name: 'red', index: 'red', width: 10, sortable: false, search: false, formatter: redFormat},
              </c:if>
              {name: 'no', index: 'no', width: 40, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'customer', index: 'customer',width: 60, sortable: true, search: true, stype: 'text', searchoptions: {sopt: ['cn']}},
              {name: 'contract', index: 'contract', width: 60, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'issuedDate', index: 'issuedDate', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'deliverDate', index: 'deliverDate', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              <c:if test="${viewtype==1}">
              {name: 'operator', index: 'operator', width: 40, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'state', index: 'state', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              </c:if>
              <c:if test="${viewtype==2}">
              {name: 'rate', index: 'rate', width: 60, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              {name: 'trackDate', index: 'trackDate', width: 100, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              </c:if>
              <c:if test="${viewtype==3}">
              {name: 'finishDate', index: 'finishDate', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              </c:if>
              <c:if test="${viewtype==4}">
              {name: 'suspendDate', index: 'suspendDate', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              </c:if>
              <c:if test="${viewtype==5}">
              {name: 'cancelDate', index: 'cancelDate', width: 80, sortable: true, search: true, searchoptions: {sopt: ['cn']}},
              </c:if>
              {name: 'act', index: 'act', width: 340, fixed: true, sortable: false, search: false, formatter: actFormat},
              {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
              onSelectRow: function () {
                var rowId = $("#order_implementation_table").jqGrid('getGridParam','selrow');
                $("#chooseId").val(rowId);
              }
            }
    );

  }

  function redFormat(cellvalue, options, rowObject){
    var red= rowObject.red;
    if(red){
      cellvalue = '<i class="ti-alert" style="color: red"></i>';
//      cellvalue = '<span style="color: red;font-weight: bold">！</span>';
    }else{
      cellvalue = '';
    }
    return cellvalue;
  }

  function actFormat(cellvalue, options, rowObject){
    var id = rowObject.id;
    cellvalue = '<button class="btn btn-white btn-sm" data-original-title="查看" onclick="fn_order_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button> ';
//    cellvalue += '<button class="btn btn-white btn-sm" data-original-title="订单暂停" onclick="fn_order_suspend(\'' + id + '\');"><i class="fa fa-hand-stop-o"> </i> 订单暂停</button> ';
//    cellvalue += '<button class="btn btn-white btn-sm" data-original-title="订单取消" onclick="fn_order_cancel(\'' + id + '\');"><i class="fa fa-times-circle"> </i> 订单取消</button> ';
//    cellvalue += '<button class="btn btn-white btn-sm" data-original-title="订单完成" onclick="fn_order_finish(\'' + id + '\');"><i class="fa fa-check"> </i> 订单完成</button> ';
    return cellvalue;
  }

  function fn_order_read(id) {
    loadURL('<%=path%>/com/orderEntity/read?keyId='+id, $('#page-content'));
  }

  <%--//在行内的按钮--%>
  <%--//订单完成--%>
  <%--function fn_order_finish(id) {--%>
    <%--var url = '<%=path%>/com/order/finish/'+id;--%>
    <%--__open_dialog_form('订单完成', url, function (dialogRef) {--%>
      <%--var callback = function (data) {--%>
        <%--if (data) {--%>
          <%--dialogRef.close();--%>
           <%--// 刷新jqgrid--%>
          <%--$("#order_implementation_table").trigger("reloadGrid");--%>
        <%--} else {--%>
          <%--dialogRef.enableButtons();--%>
        <%--}--%>
      <%--};--%>
      <%--if (fn_order_finish_save(callback) === false) {--%>
        <%--dialogRef.enableButtons();--%>
      <%--}--%>
    <%--});--%>
  <%--}--%>

  <%--//订单取消--%>
  <%--function fn_order_cancel(id) {--%>
    <%--__confirm_dialog("订单取消","确定要取消该订单吗？",function(){--%>
      <%--var url="<%=path%>/com/order/cancel";--%>
      <%--var params={keyId:id};--%>
      <%--__ajax_post(url,params,function(data){--%>
        <%--__toastr(data);--%>
        <%--$("#order_implementation_table").trigger("reloadGrid");--%>
      <%--});--%>
    <%--});--%>
  <%--}--%>

  <%--//订单暂停--%>
  <%--function fn_order_suspend(id) {--%>
    <%--__confirm_dialog("订单暂停","确定要暂停该订单吗？",function(){--%>
      <%--var url="<%=path%>/com/order/suspend";--%>
      <%--var params={keyId:id};--%>
      <%--__ajax_post(url,params,function(data){--%>
        <%--__toastr(data);--%>
        <%--$("#order_implementation_table").trigger("reloadGrid");--%>
      <%--});--%>
    <%--});--%>
  <%--}--%>

  <%--//订单恢复--%>
  <%--function fn_order_recovery(id) {--%>
  <%--__confirm_dialog("订单恢复","确定要恢复该订单吗？",function(){--%>
  <%--var url="<%=path%>/activiti/order/recovery";--%>
  <%--var params={keyId:id};--%>
  <%--__ajax_post(url,params,function(data){--%>
  <%--__toastr(data);--%>
  <%--$("#order_suspend_table").trigger("reloadGrid");--%>
  <%--});--%>
  <%--});--%>
  <%--}--%>


  //新建订单申请
  $("#btn_add_order").click(function(){
    loadURL('<%=path%>/com/orderEntity/input', $('#page-content'));
  });

  //顶部的新建订单跟踪
  $("#btn_track_order").click(function(){
    if($("#chooseId").val()==""||$("#chooseId").val()==null){
      __toastr_error("请选择一条订单");
    }else{
      loadURL('<%=path%>/com/orderEntity/read?keyId='+$("#chooseId").val()+"&track=1", $('#page-content'));
    }
  });

  //顶部的订单完成
  $("#btn_finish_order").click(function(){
    if($("#chooseId").val()==""||$("#chooseId").val()==null){
      __toastr_error("请选择一条订单");
    }else{
      var id=$("#chooseId").val();
      var url = '<%=path%>/com/orderEntity/finish/'+id;
      __open_dialog_form('订单完成', url, function (dialogRef) {
        var callback = function (data) {
          if (data) {
            dialogRef.close();
            // 刷新jqgrid
            $('#order_implementation_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
          } else {
            dialogRef.enableButtons();
          }
        };
        if (fn_order_finish_save(callback) === false) {
          dialogRef.enableButtons();
        }
      });
    }
  });

  //顶部的订单取消
  $("#btn_cancel_order").click(function(){
    if($("#chooseId").val()==""||$("#chooseId").val()==null){
      __toastr_error("请选择一条订单");
    }else{
      __confirm_dialog("订单取消","确定要取消该订单吗？",function(){
        var url="<%=path%>/com/orderEntity/cancel";
        var params={keyId:$("#chooseId").val()};
        __ajax_post(url,params,function(data){
          __toastr(data);
          $('#order_implementation_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
        });
      });
    }
  });

  //顶部的订单暂停
  $("#btn_suspend_order").click(function(){
    if($("#chooseId").val()==""||$("#chooseId").val()==null){
      __toastr_error("请选择一条订单");
    }else{
      __confirm_dialog("订单暂停","确定要暂停该订单吗？",function(){
        var url="<%=path%>/com/orderEntity/suspend";
        var params={keyId:$("#chooseId").val()};
        __ajax_post(url,params,function(data){
          __toastr(data);
          $('#order_implementation_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
        });
      });
    }
  });

  //顶部的订单恢复
  $("#btn_recovery_order").click(function(){
    if($("#chooseId").val()==""||$("#chooseId").val()==null){
      __toastr_error("请选择一条订单");
    }else{
      __confirm_dialog("订单恢复","确定要恢复该订单吗？",function(){
        var url="<%=path%>/com/orderEntity/recovery";
        var params={keyId:$("#chooseId").val()};
        __ajax_post(url,params,function(data){
          __toastr(data);
          $('#order_implementation_table').jqGrid().setGridParam({datatype:'json'}).trigger('reloadGrid');
        });
      });
    }
  });

</script>